package cn.liuguohai.dianwang.utils;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by 刘国海 on 2017/5/24.
 */

public class HttpUtil {
    public static void get(String url, Map<String, String> params, Callback callback) {
        OkHttpUtils
                .get()
                .url(url)
                .params(params)
                .build().execute(callback);
    }

    public static void post(String url, Map<String, String> params, Callback callback) {
        OkHttpUtils
                .post()
                .url(url)
                .params(params)
                .build().execute(callback);
    }

    public static void postString(String url, String gson, Callback callback) {
        OkHttpUtils
                .postString()
                .url(url)
                .content(gson)
                .build().execute(callback);
    }

    //上传文件
    public static void postFile(String url, File file, Callback callback) {
        OkHttpUtils
                .postFile()
                .url(url)
                .file(file)
                .build().execute(callback);
    }

    //上传文件和参数
    public static void uploadFileAndParams(String url, String filename, String fileurl, File file, Map<String, String> params, Map<String, String> headers, Callback callback) {
        OkHttpUtils
                .post()
                .addFile(filename, fileurl, file)
                .params(params)
                .headers(headers)
                .url(url)
                .build().execute(callback);
    }
    //上传文件和参数
    public static void uploadFileAndParams(String url, String filename, String fileurl, File file, Map<String,String> requestParams , Callback callback) {
        OkHttpUtils
                .post()
                .addFile(filename, fileurl, file)
                .url(url)
                .params(requestParams)
                .build().execute(callback);

    }


    //展示图片
    public static void showImage(String imgurl, final ImageView mImageView) {
        OkHttpUtils
                .get()//
                .url(imgurl)//
                .build()//
                .execute(new BitmapCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Bitmap response, int id) {
                        mImageView.setImageBitmap(response);
                    }
                });
    }

    //下载文件
    public static void downloadFile(String url, FileCallBack callback) {
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(callback);
    }
}
